package rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.FormParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import model.XmlUser;

import pizza.admin.dao.UserDAO;
import pizza.admin.entity.User;

/**
 * 
 * @author Radoslav Stoyanov
 *
 */
@Path("/user")
public class UserRest {
	/**
	 * Method for logging in
	 * @param String username
	 * @param String password
	 * @return XmlUser
	 */
	@POST
	@Path("login")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public XmlUser login(
			@FormParam("username") String username,
			@FormParam("password") String password) {
		UserDAO userDAO = new UserDAO();
		User user = new User();

		user = userDAO.login(username, password, false);

		if (user.getId() != null) {
			XmlUser xmluser = new XmlUser(user);
			return xmluser;
		} else {
			return null;
		}
	}

	/**
	 * Method for registering a new user
	 * @param String name
	 * @param String surname
	 * @param String username
	 * @param String password
	 * @param String passwordRepeat
	 * @return XmlUser
	 */
	@POST
	@Path("register")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public XmlUser register(@FormParam("name") String name,
			@FormParam("surname") String surname,
			@FormParam("username") String username,
			@FormParam("password") String password,
			@FormParam("password_repeat") String passwordRepeat) {
		UserDAO userDAO = new UserDAO();
		User user = new User();
		
		user.setName(name);
		user.setSurname(surname);
		user.setUsername(username);
		user.setPassword(password);
		user.setPasswordRepeat(passwordRepeat);
		user.purifyFields();
		
		if(!user.validateForDB()){
			return null;
		} else {
			userDAO.add(user);
			XmlUser xmluser = new XmlUser(user);
			return xmluser;
		}
	}
	
	/**
	 * Edits user by given ID
	 * 
	 * @param Ingeter id
	 * @param String name
	 * @param String surname
	 * @param String username
	 * @param String password
	 * @param String passwordRepeat
	 * @return XmlUser
	 */
	@POST
	@Path("edit")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public XmlUser edit(@FormParam("id") Integer id,
			@FormParam("name") String name,
			@FormParam("surname") String surname,
			@FormParam("username") String username,
			@FormParam("password") String password,
			@FormParam("password_repeat") String passwordRepeat){
		
		UserDAO userDAO = new UserDAO();
		User user = userDAO.getById(id);
		String oldPass = user.getPassword();
		
		user.setName(name);
		user.setSurname(surname);
		user.setUsername(username);
		user.setPassword(password);
		user.setPasswordRepeat(passwordRepeat);
		user.purifyFields();
		
		if(!user.validateForDB()){
			return null;
		} else {
			if(!user.getPassword().isEmpty()){
				user.encodePasswordForDB();
			}else{
				user.setPassword(oldPass);
			}
			
			userDAO.edit(user);
			XmlUser xmluser = new XmlUser(user);
			return xmluser;
		}
	}
}